'''
A module consisting of ORM-persistable classes representing the database structure, data presentation and relationships between model classes. Each class inherits from models.Model class thus gaining the ability to dynamically generate SQL queries.   
'''

# Import django modules
from django.contrib.gis.db import models
from auth.models import UserProfile

class EventType(models.Model):
    '''An ORM persistable model class representing an event type. A type is required to save an event in the databse.'''
    name = models.CharField(max_length=64)
    
    def __unicode__(self):
        return '%s' % (self.name)

class Tag(models.Model):
    '''An ORM persistable model class representing a tag. The tags are used to simplify and enhance searching for events.'''
    name = models.CharField(max_length=32) 

class Event(models.Model):
    '''An ORM persistable model class representing an event.'''
    name = models.CharField(max_length=32)
    description = models.CharField(max_length=512)
    organiser = models.CharField(max_length = 64)

    location = models.PointField(srid=4326)
    user = models.ForeignKey(UserProfile, related_name='events')   #User Profile  - our implementation of django's User
    event_type = models.ForeignKey(EventType)
    tags = models.ManyToManyField(Tag)
    location_name= models.CharField(max_length = 64)
    time = models.DateTimeField()
    total_slots = models.IntegerField()
    available_slots = models.IntegerField()
    price = models.FloatField()
    more_info_link = models.CharField(max_length = 64)
    rating = models.FloatField()
    comments = models.ManyToManyField(UserProfile, through='Comment', related_name='comments')
    subscriptions = models.ManyToManyField(UserProfile, through='Subscription', related_name='subscriptions')
    ratings = models.ManyToManyField(UserProfile, through='Rating', related_name='ratings')
    # 2 ostatnie linijki nieskonczone!!
    
    
    # find out what the 'related_name' field really does!
    #if the admin panel does't work, you migh want to add his information to the userprofile table
    objects = models.GeoManager()   #geidjango needs this. fill the somment - what does it do?
    

    def __unicode__(self):
        return '%s %s %s' % (self.name, self.location.x, self.location.y)
    
  
    
class Comment(models.Model):
    '''An ORM persistable model class representing a comment. Represents a many-to-many relation between an user and en event. Comments can be added in the \'details\' view.'''
    user = models.ForeignKey(UserProfile)
    event = models.ForeignKey(Event)
    content = models.TextField()
    
class Subscription(models.Model):
    '''An ORM persistable model class representing a subscription. An user can subscribe to an event. This means he will be notified about any changes concerning said event.'''
    user = models.ForeignKey(UserProfile)
    event = models.ForeignKey(Event)
    date = models.DateTimeField()
    
class Rating(models.Model):
    '''An ORM persistable model class representing a rating. An event can be rated by an user in the details view'''
    user = models.ForeignKey(UserProfile)
    event = models.ForeignKey(Event, related_name = 'user_mark')
    mark = models.IntegerField()

    

    
